<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class IndexController extends Controller {

    //首页
    public function index() {
        $data = \DB::table("types")->where("pid", 0)->get();
        $host = \DB::table("goods")->where("is_host", 0)->take(5)->get(); //热销产品
        $p = \DB::table("goods")->where("cid", 24)->take(5)->get(); //配件产品
        return view('home.index')->with("data", $data)->with("host", $host)->with("p", $p);
    }

    //列表页
    public function g_list($id) {
        $data = \DB::table("types")->where("pid", 0)->get();
        $datas = \DB::table("goods")->where("cid", $id)->get();
        return view('home.list')->with("data", $data)->with('datas', $datas);
    }

    //详情页
    public function xiangqing($id) {
        $data = \DB::table("types")->where("pid", 0)->get();
        $info = \DB::table("goods")->where("id", $id)->first();
        return view('home.xiangqing')->with("data", $data)->with("info", $info);
    }

    //注册页面
    public function register() {
        return view('home.register');
    }

    //注册
    public function regist(request $request) {
        //获取数据
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        $userInput = $arr["code"];
        unset($arr['code']);
        //验证码验证 
        if (Session::get('milkcaptcha') == $userInput) {
            //用户输入验证码正确  
        } else {
            //用户输入验证码错误
            echo 2;
            exit();
        }
        $rule = [
            'name' => 'required|unique:admin|between:6,12',
            'pass' => 'required|same:repass|between:6,12',
        ];
        $message = [
            'name.required' => "请输入用户名",
            'name.unique' => "用户名已经存在",
            'name.between' => "用户名长度应在6-12个字符之间",
            'pass.required' => "请输入密码",
            'pass.same' => "两次密码输入不一致",
            'pass.between' => "密码长度应在6-12个字符之间",
        ];
        $validator = \Validator::make($arr, $rule, $message);
        if ($validator->passes()) {
            $arr['time'] = time();
            unset($arr['repass']);
            if (\DB::table("user")->insert($arr)) {
                return 1;
            }
        } else {
            return $validator->getMessageBag()->getMessages();
        }
    }

    //登陆页面 
    public function login() {
        return view('home.login');
    }

    //登陆
    public function log(request $request) {
        //获取数据
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        $userInput = $arr["code"];
        unset($arr['code']);
        //验证码验证 
        if (Session::get('milkcaptcha') == $userInput) {
            //用户输入验证码正确 
        } else {
            //用户输入验证码错误
            echo 2;
            exit;
        }
        $rule = [
            'name' => 'required|between:6,12',
            'pass' => 'required|between:6,12',
        ];
        $message = [
            'name.required' => "请输入用户名",
            'name.between' => "用户名长度应在6-12个字符之间",
            'pass.required' => "请输入密码",
            'pass.between' => "密码长度应在6-12个字符之间",
        ];
        $validator = \Validator::make($arr, $rule, $message);
        if ($validator->passes()) {
            $data = \DB::table("user")->where(["name" => $arr["name"], "pass" => $arr["pass"], "status" => "0"])->first();
            if ($data) {
                session(['home_user' => $arr["name"]]);
                session(['home_userid' => $data->id]);
                echo 1; //验证成功跳转到首页  
            }
        } else {
            return $validator->getMessageBag()->getMessages();
        }
    }

    //购物车页面
    public function cart() {
        $data = \DB::table("s_cart")->where("uid", session('home_userid'))->get();
        $cot = \DB::select('select count(*) as m from s_cart');
        return view("home.cart")->with("data", $data)->with("cot", $cot[0]->m);
    }

    //加入购物车
    public function InsertCart($id) {
        $uid = session('home_userid');
        $data = \DB::table("goods")->where("id", $id)->first();
        $arr['uid'] = $uid;
        $arr['gid'] = $id;
        $data = \DB::table("s_cart")->where(array("uid" => $uid, 'gid' => $id))->first();
        if ($data) {
            \DB::table("s_cart")->where(array("uid" => $uid, 'gid' => $id))->update(array("num" => $data->num + 1));
            echo 1;
            exit;
        }
        $arr['price'] = $data->price;
        $arr['img'] = $data->img;
        $arr['num'] = 1;
        $arr['title'] = $data->title;
        $arr['createtime'] = time();
        if (\DB::table("s_cart")->insert($arr)) {
            echo 1;
        }
    }

    //购物车商品数量的改变
    public function ChangeNum($id) {
        $arr['num'] = $_POST['num'];
        \DB::table("s_cart")->where("id", $id)->update($arr);
    }

    public function orders($ids) {
//        $ids=rtrim($_GET['ids'],",");
            $arr=explode(",",$ids);  
            $order_info=\DB::table("s_cart")->whereIn("id",$arr)->get(); 
            $data = \DB::table("types")->where("pid", 0)->get();
            return view("home.orders")->with("data", $data)->with("order_info",$order_info); 
    }
    public function addr(){
        echo 111111; 
    }

}
